package com.fhz.day006;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button insert;
    private Button query;
    AppDatabase appDatabase;
    UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        insert = (Button) findViewById(R.id.insert);
        query = (Button) findViewById(R.id.query);
        appDatabase = AppDatabase.getInstance();
        userDao = appDatabase.userDao();
        userDao.getAllUserByLiveData().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                for (User user : users) {
                    System.out.println(user.uid);
                }
            }
        });
    }

    public void insert(View view) {
        User user = new User();
        user.name = "xiaoming";
        user.address = "tianjin";
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                userDao.insert(user);
//            }
//        }).start();
        userDao.insert(user);
    }

    public void query(View view) {
//        List<User> all = userDao.findAll();
        List<User> all = userDao.findAll("xiao%");
        for (User user : all) {
            System.out.println(user.uid+user.name+user.address);
        }
    }

    public void update(View view) {
        //更新必须是id匹配的.
        User user = new User();
//        user.uid = 1;  //必须加
        user.name="大明";
        user.address = "北京";
        userDao.update(user);
    }


    public void delete(View view) {
        User user = new User();
        user.uid = 1;
        userDao.delete(user);


    }
}